package com.ipt.frm.pw;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class RequestInfo
 */
public class RequestInfo extends ServletMaestro {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RequestInfo() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<body>");
		out.println("<head>");
		String titulo = request.getParameter("title");
		out.println("<title>" + titulo + "</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h3>Request Information Example</h3>");
		out.println("Method: " + request.getMethod() + "<br />");
		out.println("Request URI: " + request.getRequestURI() + "<br />");
		out.println("Protocol: " + request.getProtocol() + "<br />");
		out.println("PathInfo: " + request.getPathInfo() + "<br />");
		out.println("Remote Address: " + request.getRemoteAddr() + "<br />");

		out.println("<h3>Parametros enviados</h3>");
		out.println("Parametro[nombre]: " + request.getParameter("nombre")
				+ "<br/>");

		out.println("<h3>Parametros dinamicamente: </h3>" + "<br/>");

		Map params = request.getParameterMap();
		Iterator it = params.entrySet().iterator();

		while (it.hasNext()) {
			Map.Entry e = (Map.Entry) it.next();

			out.println("[" + e.getKey() + "] : " + e.getValue() + "<br/>");

		}

		out.println("<h3>Parametros de cabecera: </h3>" + "<br/>");
		Enumeration e = request.getHeaderNames();
		while (e.hasMoreElements()) {
			String name = (String) e.nextElement();
			String value = request.getHeader(name);
			out.println(name + " = " + value + "<br/>");
		}

		out.println("<h3>Variables de session : </h3>" + "<br/>");
		HttpSession session = request.getSession(true);
		// guardar en session Atributo recogido en formulario
		session.setAttribute("user", request.getParameter("nombre"));

		// print session info

		Date created = new Date(session.getCreationTime());
		Date accessed = new Date(session.getLastAccessedTime());
		out.println("ID " + session.getId());
		out.println("Created: " + created);
		out.println("Last Accessed: " + accessed);

		out.println("</body>");
		out.println("</html>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// puenteado las peticiones POST a doGet
		doGet(request, response);
	}

}
